﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OAProject.Domain.Interfaces;
using Dapper;
using System.Data;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;

namespace OAProject.Infrastructure.Data
{
    public class DapperRepository<T> : IRepository<T> where T : class
    {
        //protected受保护的
        protected string ConnStr = "";

        protected IDbConnection Conn;
        //依赖注入

        public int Delete(int id)
        {
            return Conn.Delete<T>(id);
        }

        public T GetById(int id)
        {
            return Conn.Get<T>(id);
        }

        public IEnumerable<T> GetList(string whereCondition = "", object paras = null)
        {
            return Conn.GetList<T>(whereCondition,paras);
        }

        public IEnumerable<T> GetPageList(int pageIndex, int pageSize, out int totalCount, string whereCondition = "", object paras = null)
        {
            //返回总条数
            totalCount = Conn.RecordCount<T>(whereCondition,paras);

            //分页结果集
            return Conn.GetListPaged<T>(pageIndex, pageSize, whereCondition, "", paras);
        }

        public int Insert(T obj)
        {
            return Conn.Insert<T>(obj) ?? -1;//??语法的意思是：当前面表示式为null的时候，返回？？后面的值;
        }

        public List<T> Show()
        {
            return Conn.GetList<T>().ToList();
        }

        public int Update(T obj)
        {
            return Conn.Update<T>(obj);
        }
    }
}
